package com.example.qxfw.dysjrh.controller;

import cn.hutool.core.io.FileUtil;
import com.example.qxfw.common.SysResult;
import com.example.qxfw.dysjrh.config.DmgisConfig;
import com.example.qxfw.dysjrh.entity.*;
import com.example.qxfw.dysjrh.service.NumericaService;
import com.example.qxfw.dysjrh.utils.GridUtil;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.http.ContentDisposition;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.io.File;
import java.io.IOException;
import java.util.*;

@RestController
@RequiredArgsConstructor
@RequestMapping("/numerica")
public class NumericaController {

    private final NumericaService numericaService;

    private final GridUtil gridUtil;

    @RequestMapping("/getNumerica")
    public SysResult getNumerica(NumData numData) {
        Object result = numericaService.getNumerica(numData);
        if (result == null ||
                result.equals("") ||
                (result instanceof String && ((String) result).length() == 0) ||
                (result instanceof Collection && ((Collection<?>) result).isEmpty())) {
            String result1 = "文件不存在";
            return SysResult.success(result1);
        }
        return SysResult.success(result);
//        return SysResult.success(numericaService.getNumerica(numData));
    }



    //查询要素
    private static final Map<String, String> maps = new HashMap<>();

    static {
        maps.put("降水量", "TPE");
        maps.put("K指标", "KIDX");
        maps.put("SI指数", "SWID");
        maps.put("CAPE", "CAPE");
        maps.put("比湿", "SHU");
        maps.put("地面气压", "PRS");
        maps.put("海平面气压", "SSP");
        maps.put("500hPa温", "500T");
        maps.put("700hPa温", "700T");
        maps.put("850hPa温", "850T");
        maps.put("500hPa高", "500GPH");
        maps.put("700hPa高", "700GPH");
        maps.put("850hPa高", "850GPH");
        maps.put("500hPa风", "500uv.u");
        maps.put("700hPa风", "700uv.u");
        maps.put("850hPa风", "850uv.u");
        maps.put("500hPa湿", "500R");
        maps.put("700hPa湿", "700R");
        maps.put("850hPa湿", "850R");
        maps.put("500hPa垂直速度", "500VVM");
        maps.put("700hPa垂直速度", "700VVM");
        maps.put("850hPa垂直速度", "850VVM");
        maps.put("500hPa水汽通量", "500MOFU");
        maps.put("700hPa水汽通量", "700MOFU");
        maps.put("850hPa水汽通量", "850MOFU");
        maps.put("500hPa涡度", "500RVO");
        maps.put("700hPa涡度", "700RVO");
        maps.put("850hPa涡度", "850RVO");
        maps.put("500hPa水汽通里散度", "500MFVO");
        maps.put("700hPa水汽通里散度", "700MFVO");
        maps.put("850hPa水汽通里散度", "850MFVO");
    }

    private final DmgisConfig dmgisConfig;

    @RequestMapping(value = "/mapView")
    @ApiOperation("气象信息-精细化预报-gird成图")
    public SysResult mapView(NumData numData) throws IOException {
        List<QxGridFile> qxGridFile1 = numericaService.queryListByDateCharTimeCharNtimesType(numData);

        if (qxGridFile1 == null || qxGridFile1.size() == 0){
            String result1 = "文件不存在";
            return SysResult.success(result1);
        }
        String nTimes = numData.getNTimes();

        List<String> list = new ArrayList<>();

        for (QxGridFile qxGridFile : qxGridFile1) {
            String name = numData.getDateChar() + "_" + numData.getTimeChar() + "_" + numData.getNTimes() + "_" + numData.getType();
            HttpHeaders headers = new HttpHeaders();
            headers.setContentType(MediaType.IMAGE_PNG); // 设置为图片的MIME类型
            headers.setContentDisposition(ContentDisposition.builder("attachment").filename(name + ".png").build());
            if (qxGridFile == null)
                return SysResult.success(list);
            String paramtype = numData.getType();
            String type = numData.getType();
            if (type.equals("ER03"))//雨
            {
                paramtype = "雨量";
            } else if (type.equals("ERH")) {
                paramtype = "湿度";
            } else if (type.equals("ECT")) {
                paramtype = "云量";
            } else if (type.equals("EDA10.U")) {
                paramtype = "风速";
            } else if (type.equals("TMP")) {
                paramtype = "温度";
            }
//            else if (type.equals("500hPa温")  || type.equals("700hPa温") || type.equals("850hPa温") ) {
//                paramtype = "温度";
//            } else if (type.equals("500hPa高")  || type.equals("700hPa高") || type.equals("850hPa高") ) {
//                paramtype = "高度";
//            } else if (type.equals("500hPa风") || type.equals("700hPa风") || type.equals("850hPa风") ) {
//                paramtype = "风速";
//            } else if (type.equals("500hPa湿") || type.equals("700hPa湿") || type.equals("850hPa湿")) {
//                paramtype = "湿度";
//            }
            else if (type.equals("降水量")&&nTimes.equals("003")) {
                paramtype = "3小时雨量";}
            else if (type.equals("降水量")&&nTimes.equals("024")) {
                paramtype = "24小时雨量";
            } else if (type.equals("地面气压")) {
                paramtype = "地面气压场";
            }else if (type.equals("海平面气压")) {
                paramtype = "海平面气压声";
            }

            List<ContourSetting> contourSetting = numericaService.getContourSetting(paramtype);

//        byte[] data = gridUtil.processGridDataPic(qxGridFile,contourSetting);
            GridDataPicAndXY gridDataPicAndXY = gridUtil.processGridDataPicAndXY(qxGridFile, contourSetting,dmgisConfig.getGridFilePath());

            if (gridDataPicAndXY == null) {
                String result1 = "文件不存在";
                return SysResult.success(result1);
            }

            byte[] filedata = gridDataPicAndXY.getFiledata();

            String collectionName = "";
            if ("EC".equals(numData.getNumberType())){
                collectionName = "grid_micaps_file";
            }else {
                collectionName = "grid_cmameso_file";
            }

            String path2 = "DmgisPubData/dmgis_xjservice_file/" + "/" +collectionName
                    + "/" + numData.getDateChar() + "/" + numData.getTimeChar() + "/" + numData.getNTimes() + "/" + maps.get(numData.getType())+ ".png";


            String fileName = dmgisConfig.getTxtFilePath() + path2;

            //删除文件
            FileUtil.del(fileName);
            //创建文件
            FileUtil.touch(fileName);
            FileUtil.writeBytes(filedata, new File(fileName));


            String imagePath = dmgisConfig.getNgfileUrl() + path2;

            list.add(imagePath);

        }
        return SysResult.success(list);
    }


}
